import random
import re
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import datetime
import math

def get_driver(country):
    opt = webdriver.ChromeOptions()
    # 设定下载文件的保存目录为d盘的tudi目录，
    # 如果该目录不存在，将会自动创建
    download_path = 'E:\\download\\' + country#输入本地下载路径
    prefs = {"download.default_directory": download_path,
             "profile.default_content_setting_values.automatic_downloads": 1}
    # 将自定义设置添加到Chrome配置对象实例中
    opt.add_experimental_option("prefs", prefs)
    opt.add_experimental_option('useAutomationExtension', False)
    opt.add_argument('--disable-blink-features=AutomationControlled')
    opt.add_experimental_option('excludeSwitches', ['enable-automation'])#去掉“chrome正受到自动测试软件的控制”提示
    chromedriver = r'C:\Program Files\Google\Chrome\Application\chromedriver'#输入Chromedriver路径
    driver = webdriver.Chrome(executable_path=chromedriver, options=opt)
    # 防止被监测
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined});""",
    })
    driver.implicitly_wait(10)
    handles = driver.window_handles
    driver.switch_to.window(handles[0])
    driver.maximize_window()
    return driver

def download_file(driver):
    #back是否存在
    back_iframe = driver.find_elements(By.XPATH, '//iframe[@class = "sys-overlay-style"]')
    if len(back_iframe) > 0:
        driver.switch_to.frame(back_iframe[0])
        back_btn = driver.find_element(By.XPATH,r'//button[text() = "Back"]')
        driver.execute_script("arguments[0].click();", back_btn)
    # 下载
    download = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//button[text() = "Download"]')))
    driver.execute_script("arguments[0].click();", download)
    time.sleep(2)
    # swich到iframe
    iframe = driver.find_element(By.XPATH,'//iframe[@class = "sys-overlay-style"]')
    driver.switch_to.frame(iframe)
    # Augur pipeline
    # WebDriverWait(driver, 30, 0.5).until(EC.presence_of_element_located((By.XPATH, '//input[text()="Input for the Augur pipeline"]')))
    Augur = driver.find_element(By.XPATH, '//input[contains(@value,"augur_input")]')
    driver.execute_script("arguments[0].click();", Augur)
    # download
    # resp = driver.page_source
    # time.sleep(5)
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '/html/body/form/div[5]/div/div[2]/div/div/div[2]/div/button')))
    download_btn = driver.find_element(By.XPATH, '/html/body/form/div[5]/div/div[2]/div/div/div[2]/div/button')
    driver.execute_script("arguments[0].click();", download_btn)
    # 回到原始网页
    driver.switch_to.default_content()
    time.sleep(3)
    # 等待下载完成
    WebDriverWait(driver, 180).until_not(EC.element_to_be_clickable((By.XPATH, '//div[contains(text(),"Preparing Download")]')))

if __name__ == '__main__': #主函数，代码会从这开始运行
    #读取文件国家数
    start_date = '2020-01-01'
    end_date = '2021-12-31'
    file_path = '国家名称.csv'
    df = pd.read_csv(file_path)
    country_list = list(df.country.values)
    # country_list = ['Global']
    for country in country_list:
        try:
            driver = get_driver(country)
            url = r'https://www.epicov.org/epi3/frontend#569ff2'
            driver.get(url)
            #输入账号密码
            user_name = driver.find_element(By.XPATH,'//input[@id="elogin"]')
            user_name.send_keys('输入账号')
            password = driver.find_element(By.XPATH,'//input[@id="epassword"]')
            password.send_keys('输入账号密码')
            #登录
            time.sleep(1)
            WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//input[@class = "form_button_submit"]')))
            login_btn = driver.find_element(By.XPATH,'/html/body/form/div[5]/div/div[2]/div/div/div/div[1]/div/div/div[2]/input[3]')
            driver.execute_script("arguments[0].click();", login_btn)
            # login_btn.click()
            time.sleep(random.randint(1,3))
            #等待出现searh按钮
            WebDriverWait(driver, 30, 0.5).until(EC.presence_of_element_located((By.XPATH, '//div[@class = "sys-actionbar-bar"]/div[@class = "sys-actionbar-action-ni"][2]')))
            time.sleep(1)
            search_btn = driver.find_element(By.XPATH,'//div[@class = "sys-actionbar-bar"]/div[@class = "sys-actionbar-action-ni"][2]')
            driver.execute_script("arguments[0].click();", search_btn)
            time.sleep(2)
            driver.implicitly_wait(20)
            # WebDriverWait(driver, 30, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="ce_r40kme_ar_entry"]')))
            #输入国家
            location_input = driver.find_element(By.XPATH,'//*[@id="ce_r99k81_ai_entry"]')
            location_input.clear()
            location_input.send_keys(country)
            time.sleep(1)
            #选择日期
            date_start = driver.find_element(By.XPATH, '//*[@id="ce_r9lyrr_au_input"]')
            date_end = driver.find_element(By.XPATH, '//*[@id="ce_r9lyrr_av_input"]')
            date_start.send_keys(start_date)
            time.sleep(1)
            date_end.send_keys(end_date)
            time.sleep(1)
            driver.execute_script("arguments[0].click();", date_end)
            time.sleep(5)
            #complete
            complete_checkbox = driver.find_element(By.XPATH,'//*[@id="ce_r9lyrr_b3"]/div[1]/div/input')
            driver.execute_script("arguments[0].click();", complete_checkbox)
            time.sleep(2)
            #high coverage
            coverage_checkbox = driver.find_element(By.XPATH,'//*[@id="ce_r9lyrr_b4"]/div[1]/div/input')
            driver.execute_script("arguments[0].click();", coverage_checkbox)
            #搜索
            # location_input.send_keys(Keys.ENTER)
            date_end.send_keys(Keys.ENTER)
            driver.implicitly_wait(20)
            # time.sleep(10)
            waiting_xpath = r'//table[contains(@style,"margin-top: 0px; width: 1132px;")]'
            WebDriverWait(driver, 60, 0.5).until_not(EC.presence_of_element_located((By.XPATH, waiting_xpath)))
            # 读取总数量，计算得到页面数量
            driver.refresh()
            # time.sleep(5)
            total_text = driver.find_element(By.XPATH,'/html/body/form/div[5]/div/div[2]/div/div[2]/div[2]/div[2]/div[1]/span/span').text
            all_numbers = re.findall(r'\d',total_text)
            total_num = ''.join(all_numbers)
            total_page = math.ceil(int(total_num)/50)
            print(country,total_num,total_page)
            #如果总数小于5000直接全选下载
            if float(total_num) <= 5000:
                # 选中所有
                checkbox = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '/html/body/form/div[5]/div/div[2]/div/div[2]/div[2]/div[1]/div[2]/table/thead/tr/th[1]/div/span/input')))
                driver.execute_script("arguments[0].click();", checkbox)
                #进行文件下载
                download_file(driver)
            #如果大于5000，则进行分批下载
            else:
                total_page = int(total_page)
                time.sleep(random.randint(3, 5))
                for i in range(0,total_page):
                    #断点续下载
                    if i > 1-1:
                        select_box_list = driver.find_elements(By.XPATH, '//input[@class = "yui-dt-checkbox" and contains(@type,"checkbox")]')
                        for index in range(len(select_box_list)):
                            select_box_list = driver.find_elements(By.XPATH,'//input[@class = "yui-dt-checkbox" and contains(@type,"checkbox")]')
                            item = select_box_list[index]
                            ActionChains(driver).move_to_element(item).perform()
                            driver.execute_script("arguments[0].click();", item)
                            time.sleep(random.random())
                        time.sleep(3)
                        download_file(driver)
                        print(i+1,"页下载完成")
                        time.sleep(10)
                    #点击下一页
                    WebDriverWait(driver, 30, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@class="yui-pg-next"]')))
                    next_btn = driver.find_element(By.XPATH, '//*[@class="yui-pg-next"]')
                    driver.execute_script("arguments[0].click();", next_btn)
                    driver.refresh()
        except:
            print('%s下载失败'%country)
            driver.quit()
            continue
    driver.quit()
    print('所有下载完成')
